<?php
    header('Content-Type:text/html;charset=utf-8;');
    
    /*建立连接*/
    $conn = mysqli_connect('localhost', 'root', '123', 'buka');
    /*设置编码*/
    mysqli_set_charset($conn, 'utf8');
    /*查询所有游戏数据*/
    function selectAllGame()
    {
        global $conn;
        $sql = "SELECT * FROM gameCenter";
        /*获取数据资源*/
        $res = mysqli_query($conn, $sql);
        $dataArr = array();
        /*一行一行的读取*/
        while ($row = mysqli_fetch_assoc($res)) {
            array_push($dataArr, $row);
        }
        return $dataArr;
    }

    /*查询所有漫画数据*/
    function selectAllBook()
    {
        global $conn;
        // $sql = "SELECT dena.id,name,author,categoryId,newCharpter,lastUpdate,pic,category.cName FROM dena,category where dena.categoryId = category.id ORDER BY lastUpdate desc limit 12";

        // ORDER BY d.lastUpdate desc   d.lastUpdate 按照降序排序   desc(降序) asc(升序)
        $sql = "SELECT d.id,d.name,d.author,d.newCharpter,d.pic,d.lastUpdate FROM dena d ORDER BY d.lastUpdate desc limit 12";
        /*获取数据资源*/
        $res = mysqli_query($conn, $sql);
        $dataArr = array();
        /*一行一行的读取*/
        while ($row = mysqli_fetch_assoc($res)) {
            array_push($dataArr, $row);
        }
        return $dataArr;
    }
    /*查询所有用户收藏数据*/
    function selectAllFav()
    {
        global $conn;
        // $sql = "SELECT mylove.id,mylove.bookid,bookName,isStar,pic FROM myLove,dena where dena.id = mylove.bookId";
        $sql = "SELECT m.id,m.bookid,m.bookName,m.isStar,d.pic FROM myLove m LEFT JOIN dena d ON d.id = m.bookId";
        /*获取数据资源*/
        $res = mysqli_query($conn, $sql);
        $dataArr = array();
        /*一行一行的读取*/
        while ($row = mysqli_fetch_assoc($res)) {
            array_push($dataArr, $row);
        }
        return $dataArr;
    }

    /*查询指定的漫画详细数据*/
    function selectBookById($id)
    {
        global $conn;
        // $sql = "select dena.id as id,name,author,newCharpter,lastUpdate,intro,bookDetail.pic as pic,score from dena,bookdetail where dena.id = bookdetail.bookId and dena.id = " . $id;
        $sql = "select * FROM dena WHERE id=" . $id;
        /*获取数据资源*/
        $res = mysqli_query($conn, $sql);
        /*通过id查询只能查询到一条记录，所以没有必要再进行循环遍历，只是需要读取这一行数据返回*/
        $row = mysqli_fetch_assoc($res);
        return $row;
    }
